package com.cfets.annualAssessment.service;

import com.cfets.annualAssessment.entity.Account;
import com.cfets.annualAssessment.entity.AccountRole;
import com.cfets.annualAssessment.mapper.AccountMapper;
import com.cfets.annualAssessment.mapper.AccountRoleMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created with UserDetailService
 * User: pual
 * Date: 2016/10/12
 * Desc:
 */
@Service
public class AccountService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AccountRoleMapper accountRoleMapper;

    @Autowired
    private AccountMapper accountMapper;

    public Account getAccountByUserName(String name){
        Account account = accountMapper.selectByName(name);
        return account;
    }

    public Account getAccountById(long id) {
        return accountMapper.selectByPrimaryKey((int)id);
    }

    public Account newAccount(Account account) {
        account.setPassword(passwordEncoder.encode(account.getNewpassword()));
        if(account.getRolename()!=0){
            AccountRole accountRole = new AccountRole();
            accountRole.setRole_id(account.getRolename());
            accountRole.setAccount_id(account.getId());
            accountRoleMapper.insertUseGeneratedKeys(accountRole);
        }
        accountMapper.insertUseGeneratedKeys(account);
        return account;
    }

    public boolean deleteAccount(long id){
        List<AccountRole> accountRoles = null;
        AccountRole accountRole = new AccountRole();
        accountRole.setAccount_id(id);
        accountRoles = this.getAccountRoleList(accountRole);
        for (AccountRole item:accountRoles){
            accountRoleMapper.deleteByPrimaryKey(item.getId());
        }
        return accountMapper.deleteByPrimaryKey(id)>0;
    }

    public Account updateAccount(Account account) {
        if(!StringUtils.isEmpty(account.getNewpassword())) {
            account.setPassword(passwordEncoder.encode(account.getNewpassword()));
        }
        accountMapper.updateByPrimaryKey(account);
        return account;
    }

    public List<Account> getAccountAll() {
        return  accountMapper.selectAll();
    }

    public List<Account> getAccountPage(Account account) {
        Page<?> page = PageHelper.startPage(account.getPage(), account.getRows());
        List<Account> accountList=null;
        accountList = this.getAccountList(account);
        account.setTotalCount(page.getTotal());
        return accountList;
    }

    public List<Account> getAccountList(Account account){
        List<Account> accountList=null;
        Example example=new Example(Account.class);
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(account.getAccountName())){
//            example = new Example(Account.class);
            criteria.andEqualTo("accountName",account.getAccountName());
        }
        if(!StringUtils.isEmpty(account.getUsername())){
//            example=example!=null?example:new Example(Account.class);
            criteria.andLike("username","%"+account.getUsername()+"%");
        }
        if(account.getStatus()==1||account.getStatus()==2||account.getStatus()==3){
            criteria.andEqualTo("status",account.getStatus());
        }
        if(criteria!=null){
            accountList = accountMapper.selectByExample(example);
        }else {
            accountList = accountMapper.selectAll();
        }
        return accountList;
    }

    public List<AccountRole> getAccountRoleList(AccountRole accountRole){
        List<AccountRole> accountRoleList=null;
        Example example=new Example(AccountRole.class);
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(accountRole.getAccount_id())){
            criteria.andEqualTo("account_id",accountRole.getAccount_id());
        }
        if(!StringUtils.isEmpty(accountRole.getRole_id())){
            criteria.andEqualTo("role_id",accountRole.getRole_id());
        }
        accountRoleList = accountRoleMapper.selectByExample(example);
        return accountRoleList;
    }
}
